package com.csv;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class CsvValidateGui {
    public static JFrame jFrame;
    public JTextArea textArea;

    public CsvValidateGui(){
        jFrame = new JFrame();
        jFrame.setTitle("NLB CSV VERIFICATION");
        jFrame.setBounds(600, 300, 500, 400);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        jFrame.getContentPane().add(panel, BorderLayout.NORTH);
        JButton button = new JButton("Select File");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser();
                jfc.showSaveDialog(jFrame);
                try{
                    File file = jfc.getSelectedFile();
                    String content = CsvValidateService.validate(file.toString());
                    textArea.setText(content);
                    if("success".equals(content)){
                        JOptionPane.showMessageDialog(null, "Congratulation to you that passed nlb validation");
                    }else{
                        JOptionPane.showMessageDialog(null, content, "Verification failed", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (Exception e2){
                    JOptionPane.showMessageDialog(null, "Not select any file", "Notice", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        panel.add(button);
        JScrollPane scrollPane = new JScrollPane();
        jFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        jFrame.setVisible(true);
    }
}
